package com.sebiotec.ambassador.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.lifecycle.ViewModelProviders;

import com.sebiotec.ambassador.R;
import com.sebiotec.ambassador.R2;
import com.sebiotec.ambassador.model.event.BasicEvent;
import com.sebiotec.ambassador.model.response.UseProfileResponse;
import com.sebiotec.ambassador.ui.activity.BasicActivity;
import com.sebiotec.ambassador.ui.activity.LanguageActivity;
import com.sebiotec.ambassador.ui.activity.UpdatePwdActivity;
import com.sebiotec.ambassador.viewmodel.ProfileViewModel;
import com.sebiotec.basecommon.utils.base.BaseFragment;
import com.sebiotec.basecommon.utils.entity.event.LoginEvent;

import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.GravityEnum;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ProfileFragment extends BaseFragment {


    @BindView(R2.id.tv_profile_name)
    TextView tvProfileName;
    @BindView(R2.id.textView12)
    TextView textView12;
    @BindView(R2.id.iv_photo)
    ImageView ivPhoto;
    @BindView(R2.id.cl_base)
    ConstraintLayout clBase;
    @BindView(R2.id.cl_update_pwd)
    ConstraintLayout clUpdatePwd;
    @BindView(R2.id.cl_lang)
    ConstraintLayout clLang;
    @BindView(R2.id.cl_login_out)
    ConstraintLayout clLoginOut;
    private ProfileViewModel profileViewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile, container, false);
        ButterKnife.bind(this, view);
         profileViewModel = ViewModelProviders.of(this).get(ProfileViewModel.class);
         EventBus.getDefault().register(this);
        return view;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void initData() {
        profileViewModel.getUser(new ProfileViewModel.MyBasicDataCallBack<Object>() {
            @Override
            public void onDataSuccessCallBack(Object o) {
                UseProfileResponse bean=(UseProfileResponse)o;
                tvProfileName.setText(bean.getEName());
            }

            @Override
            public void onDataFailCallBack(Object o) {

            }
        });
    }

    @OnClick({R2.id.iv_photo, R2.id.cl_base, R2.id.cl_update_pwd, R2.id.cl_lang,R2.id.cl_login_out})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.iv_photo) {
        } else if (id == R.id.cl_base) {
            startActivity(BasicActivity.class);
        } else if (id == R.id.cl_update_pwd) {
            startActivity(UpdatePwdActivity.class);
        } else if (id == R.id.cl_lang) {
            startActivity(LanguageActivity.class);
        }else if (id==R.id.cl_login_out){
            new MaterialDialog.Builder(getActivity())
                    .content(getString(R.string.islogin_out))
                    .contentGravity(GravityEnum.CENTER)
                    .negativeColor(getResources().getColor(R.color.smj))
                    .positiveColor(getResources().getColor(R.color.smj))
                    .buttonsGravity(GravityEnum.CENTER)
                    .positiveText(getString(R.string.yes))
                    .negativeText(getString(R.string.no_login))
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                            ActivityTool.getInstance().finishAllActivities();
//                          ARouter.getInstance().build("/Login/LoginActivity").navigation(getContext().getApplicationContext());

                            //发消息让Man关闭
                            EventBus.getDefault().post(new LoginEvent(true));
                        }
                    }).show();

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void event(BasicEvent event){
        tvProfileName.setText(event.getName());
    }


}
